//
// Created by 陈成 on 2019/9/19.
//

#include "Mem.h"

Mem::Mem() {

}

void Mem::write(uint64_t addr, uint8_t data) {
    mem[addr] = data;
}

void Mem::write(uint64_t addr, uint32_t data) {
    write(addr + 0, (uint8_t)((data >>  0) & 0xff));
    write(addr + 1, (uint8_t)((data >>  8) & 0xff));
    write(addr + 2, (uint8_t)((data >> 16) & 0xff));
    write(addr + 3, (uint8_t)((data >> 24) & 0xff));
}

void Mem::write(uint64_t addr, uint64_t data) {
    write(addr + 0, (uint32_t)(data >>  0 & 0xffffffff));
    write(addr + 4, (uint32_t)(data >> 32 & 0xffffffff));
}

uint8_t Mem::read1Byte(uint64_t addr) {
    return (mem.count(addr) ? mem[addr] : 0);
}

uint32_t Mem::read4Bytes(uint64_t addr) {
    uint32_t temp = 0;
    temp = (temp << 8) + read1Byte(addr + 3);
    temp = (temp << 8) + read1Byte(addr + 2);
    temp = (temp << 8) + read1Byte(addr + 1);
    temp = (temp << 8) + read1Byte(addr + 0);
    return temp;
}

uint64_t Mem::read8Bytes(uint64_t addr) {
    uint64_t temp = 0;
    temp = (temp << 32) + read4Bytes(addr + 4);
    temp = (temp << 32) + read4Bytes(addr + 0);
    return temp;
}

void Mem::clear() {
    mem.clear();
}
